package com.example.lib_base.net

import com.blankj.utilcode.util.SPUtils
import com.example.lib_base.utils.Cons
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

class RetrofitUtils {
     //线程安全
    companion object{
        @Volatile
        private var instance:RetrofitUtils? = null //定义实例 默认为空

        fun getInstance():RetrofitUtils{
            if(instance==null){
                synchronized(this){
                    instance= RetrofitUtils()
                }
            }
            return instance!!
        }
    }

    fun getRetrofit():Retrofit{
        val client = OkHttpClient.Builder()
            .readTimeout(5,TimeUnit.SECONDS)
            .writeTimeout(5,TimeUnit.SECONDS)
            .connectTimeout(5,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor(TokenInterceptor())
            .build()
        val retrofit = Retrofit.Builder()
            .client(client)
            .baseUrl(Cons.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        return retrofit
    }
}